Understanding
Classes Within Object Oriented Programming
|
Understanding Classes Within Object Oriented
Programming
A
class is a collection of objects of similar type. Once a class is defined,
any number of objects can be created which belong to that class. In
object-oriented programming, a class is a programming language construct that
is used as a blueprint to create objects. This blueprint includes attributes
and methods that the created objects all share. More
technically, a class is a cohesive package that consists of a particular kind
of metadata. It describes the rules by which objects behave; these objects
are referred to as instances of that class. A class has both an interface and
a structure. The interface describes how the class and its instances can be
interacted with via methods, while the structure describes how the data is
partitioned into attributes within an instance. A class may also have a
representation (metaobject) at runtime, which provides runtime support for
manipulating the class-related metadata. In object-oriented design, a class
is the most specific type of an object in relation to a specific layer. Classes,
when used properly, can accelerate development by reducing redundant code
entry, testing and bug fixing. If a class has been thoroughly tested and is
known to be a solid work, it is typically the case that using or extending
the well-tested class will reduce the number of bugs - as compared to the use
of freshly-developed or ad hoc code - in the final output. In addition,
efficient class reuse means that many bugs need to be fixed in only one place
when problems are discovered. A
class is used to create new instances (objects) by instantiating the class.
Instances of a class share the same set of attributes, yet may differ in what
those attributes contain. For example, a class "Person" would
describe the attributes common to all instances of the Person class. Each
person is generally alike, but varies in such attributes as
"height" and "weight". The class would list types of such
attributes and also define the actions which a person can perform:
"run", "jump", "sleep", "walk", etc.
One of the benefits of programming with classes is that all instances of a
particular class will follow the defined behavior of the class they instantiate. In
most languages, the structures as defined by the class determine how the
memory used by its instances will be laid out. This technique is known as the
cookie-cutter model. The alternative to the cookie-cutter model is the model
of Python, wherein objects are structured as associative key-value
containers. In such models, objects that are instances of the same class
could contain different instance variables, as state can be dynamically added
to the object. This may resemble prototype-based languages in some ways, but
it is not equivalent. Along
with having an interface, a class contains a description of structure of data
stored in the instances of the class. The data is partitioned into attributes
(or properties, fields, data members). Going back to the television set
example, the myriad attributes, such as size and whether it supports color,
together comprise its structure. A class represents the full description of a
television, including its attributes (structure) and buttons (interface). A
class also describes a set of invariants that are preserved by every method
in the class. An invariant is a constraint on the state of an object that
should be satisfied by every object of the class. The main purpose of the
invariants is to establish what objects belong to the class. An invariant is
what distinguishes data types and classes from each other; that is, a class
does not allow use of all possible values for the state of the object, and
instead allows only those values that are well-defined by the semantics of
the intended use of the data type. The set of supported (public) methods
often implicitly establishes an invariant. Some programming languages support
specification of invariants as part of the definition of the class, and
enforce them through the type system. Encapsulation of state is necessary for
being able to enforce the invariants of the class. Some
languages allow an implementation of a class to specify constructor (or
initializer) and destructor (or finalizer) methods that specify how instances
of the class are created and destroyed, respectively. A constructor that
takes arguments can be used to create an instance from passed-in data. The
main purpose of a constructor is to establish the invariant of the class,
failing if the invariant isn't valid. The main purpose of a destructor is to
destroy the identity of the instance, invalidating any references in the
process. Constructors and destructors are often used to reserve and release,
respectively, resources associated with the object. In some languages, a
destructor can return a value which can then be used to obtain a public
representation (transfer encoding) of an instance of a class and
simultaneously destroy the copy of the instance stored in current thread's
memory. A
class may also contain static attributes or class attributes, which contain
data that are specific to the class yet are common to all instances of the
class. If the class itself is treated as an instance of a hypothetical
metaclass, static attributes and static methods would be instance attributes
and instance methods of that metaclass. As
a data type, a class is usually considered as a compile-time construct. A
language may also support prototype or factory metaobjects that represent
run-time information about classes, or even represent metadata that provides
access to reflection facilities and ability to manipulate data structure
formats at run-time. Many languages distinguish this kind of run-time type
information about classes from a class on the basis that the information is
not needed at run-time. Some dynamic languages do not make strict
distinctions between run-time and compile-time constructs, and therefore may
not distinguish between metaobjects and classes. There
are many categories of classes depending on modifiers. Note that these
categories do not necessarily categorize classes into distinct partitions.
For example, while it is impossible to have a class that is both abstract and
concrete, a sealed class is implicitly a concrete class, and it may be
possible to have an abstract partial class.
|